package com.example.service.impl;

import com.example.pojo.sandbox.ExecuteCodeRequest;
import com.example.pojo.sandbox.ExecuteCodeResponse;
import com.example.constant.LanguageEnum;
import com.example.sandbox.CCodeSandbox;
import com.example.sandbox.CPlusPlusCodeSandbox;
import com.example.sandbox.JavaCodeSandbox;
import com.example.sandbox.PythonCodeSandbox;
import com.example.service.SandboxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SandboxServiceImpl implements SandboxService {
    // java语言的执行沙箱
    @Autowired
    private JavaCodeSandbox javaCodeSandbox;
    // python语言的执行沙箱
    @Autowired
    private PythonCodeSandbox pythonCodeSandbox;
    // C++语言的执行沙箱
    @Autowired
    private CPlusPlusCodeSandbox cPlusPlusCodeSandbox;
    // C语言的执行沙箱
    @Autowired
    private CCodeSandbox cCodeSandbox;

    // 对不同类型语言分类执行
    @Override
    public ExecuteCodeResponse compileAndRunCode(ExecuteCodeRequest executeCodeRequest) {
        String language = executeCodeRequest.getLanguage();
        if (language.equals(LanguageEnum.JAVA.getValue())) {
            return javaCodeSandbox.executeCode(executeCodeRequest);
        } else if (language.equals(LanguageEnum.PYTHON.getValue())) {
            return pythonCodeSandbox.executeCode(executeCodeRequest);
        } else if (language.equals(LanguageEnum.CPLUSPLUS.getValue())) {
            return cPlusPlusCodeSandbox.executeCode(executeCodeRequest);
        } else if (language.equals(LanguageEnum.C.getValue())) {
            return cCodeSandbox.executeCode(executeCodeRequest);
        }
        return null;
    }
}
